import {JRequest, jStorage} from 'jzutil';
import permission from './permission';
const config = {
    retry: 5,
    headers: {
        appId: jStorage.getSession('appId'),
        appKey: jStorage.getSession('appKey'),
        'iot-token': jStorage.getSession('iot-token')
    },
    beforeResponse: (e: any) => {
        jStorage.setSession('appId', '13800000001');
        jStorage.setSession('appKey', 'fcea920f7412b5da7be0cf42b8c93759');

        const token = e.headers.get('iot-token');
        if (token) {
            jStorage.setSession('iot-token', token);
        }
        return e.json();
    },
    afterResponse: (next: Function, data: any, again: Function) => {
        if (data.code === -10020) {
            again();
        } else {
            next();
        }
    },
    errorResponse: (next: Function) => {
        next();
    }
};
const jRequest = new JRequest(config);

export const Permission = jRequest.registerModule(permission);
